<template>
  <div class="login-container">
    <div class="logo-name">
      <div class="logo">
        <img src="@/assets/login_images/logo-new.png" alt="">
      </div>
      <p class="name">体育与健康运动负荷监测与评价系统</p>
    </div>

    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">欢迎登陆</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <p class="check-psd">
        <span class="check-item">
          <el-checkbox v-model="checked">记住密码</el-checkbox>
        </span>
        <span class="check-item">
          忘记密码
        </span>
      </p>

      <el-button :loading="loading" type="success" class="submit" style="" @click.native.prevent="handleLogin">登陆</el-button>

      <!-- <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div> -->

    </el-form>

    <p class="ba">备案号：154594824872154</p>
    <div class="people">
      <img src="@/assets/login_images/people.png" alt="">
    </div>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 2) {
        callback(new Error('The username can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'test1',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      checked: true
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    // console.log(this.routes)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const app = {
            appid: this.loginForm.username,
            appsecret: this.loginForm.password
          }
          this.$store.dispatch('user/login', app).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: black;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.submit{
  width:100%;margin-bottom:30px;height: 70px;
}
.people{
  z-index: 2;
  position: absolute;
  width: 533px;
  bottom: 0;
  left: 125px;
  img{
    width: 100%;
  }
}
.logo-name{
  position: absolute;
  top: 20px;
  left: 20px;
  display: flex;
  align-items: center;
  .logo{
    width: 54px;
    margin-right: 20px;
    img{
      width: 100%;
    }
  }
  .name{
    margin-top: 28px;
    font-size: 36px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: bold;
    color: #223762;
  }
}
/* reset element-ui css */
.login-container {
  position: relative;

  .el-input {
    display: inline-block;
    // height:60px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: black;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fcfdfd inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #dce0eb;
    background: #fcfdfd;
    border-radius: 5px;
    color: #454545;
  }
}
.el-form-item__content{
  // line-height: 60px;
}
</style>

<style lang="scss" scoped>
$bg:white;
$dark_gray:#889aa4;
$light_gray:#092150;

::v-deep .el-checkbox__input.is-checked .el-checkbox__inner{
  border-color: #41cb89;
  outline-color: #41cb89;
  background-color: #41cb89;
}
::v-deep .el-checkbox__input.is-checked + .el-checkbox__label{
  color: #41cb89;
}
.check-psd{
  display: flex;
  justify-content: space-between;
  .check-item{
    font-size: 14px;
    color: #3f3f3f;
  &:last-of-type{
    // color: #1890ff;
    color: #47d993;
  }
  }
}

.login-container {
  min-height: 100%;
  width: 100%;
  background: linear-gradient(164deg, #C9FDF1 0%, #D9F6F2 31%, #E1F5F6 52%, #EDFBFF 73%, #FFFEFF 100%);
  overflow: hidden;
  position: relative;
  &::after{
    content: '';
    position: absolute;
    right: 170px;
    top: 155px;
    display: block;
    background: url(../../assets/login_images/yun.png) no-repeat center center /contain;
    width: 137px;
    height: 76px;
    z-index: 9;
  }
  .login-form {
    z-index: 10;
    position: absolute;
    // width: 620px;
    // height: 800px;
    width: 440px;
    height: 516px;
    background: #FFFFFF;
    box-shadow: 10px 10px 10px 0px rgba(154, 224, 217, 0.1);
    border-radius: 20px;
    max-width: 100%;
    padding: 0  70px;
    right: 228px;
    top: 155px;
    overflow: hidden;

  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    padding-top: 60px;
    .title {
      font-size:36px;
      color: $light_gray;
      margin: 0px auto 40px 0;
      text-align: left;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 13px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.ba{
  z-index: 3;
	position: absolute;
	left: 20px;
	bottom: 0;
  width: 429px;
  height: 63px;
  font-size: 18px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  font-weight: bold;
  line-height: 63px;
  opacity: 0.8;
}

@media screen and (max-width:1400px) {
  .login-form{
    top: 363px!important;
  }
  .login-container{
    &::after{
      top: 363px!important;
    }
  }
}
@media screen and (max-width:1290px) {
// @media screen and (max-width:1200px) {
  .people{
    width: 380px;
  }
  .submit{
    height: 40px;
  }
  .login-form{
    top: auto!important;
    bottom: 100px!important;
    right: 171px!important;
    height: 380px!important;
    width: 330px!important;
    padding:0 20px!important;
    .title-container{
      padding-top: 20px!important;
    }
  }
  .login-container{
    &::after{
      top: 280px!important;
      right: 110px!important;
    }
  }
}

</style>
